<template>
  <div class="goods">
    <div class="wrap">
      <Crumb> </Crumb>
      <img src="../assets/img/banner.4c6b6225.png" width="100%" alt="" />
      <ul class="options">
        <li>
          <strong>排序：</strong>
          <span
            :class="{ active: index == optionsNum }"
            v-for="(item, index) in options1"
            :key="index"
            @click="options1Fn(index, item.min, item.max)"
            >{{ item.txt }}</span
          >
        </li>
        <li>
          <strong>分类：</strong>
          <span
            :class="{ active: item.isActive }"
            v-for="(item, index) in options2"
            :key="index"
            @click="options2Fn(index, item.type)"
            >{{ item.txt }}</span
          >
        </li>
      </ul>
      <List :arr="goodsShowList" />
      <div style="text-align: center">
        {{ isBottom ? "已经没有数据了" : "正在加载..." }}
      </div>
    </div>
  </div>
</template>                            
<script>
import Crumb from "../components/Crumb.vue";
import List from "../components/home/List.vue";
import { GoodsSearchAPI } from "@/request/api";
import { getScrollHeight, getClientHeight, getScrollTop } from "@/utils";
export default {
  data() {
    return {
      did: 0,
      type: 0,
      min: 0,
      max: 0,
      page: 1,
      size: 8,
      isLoading: false,
      isBottom: false,
      keyword: "",
      optionsNum: 0,
      goodsList: [],
      goodsShowList: [],
      options1: [
        { min: 0, max: 10000, txt: "全部" },
        // "我还可以兑换"的max值是Header组件的coin值
        { min: 0, max: 0, txt: "我还可以兑换" },
        { min: 0, max: 500, txt: "0-500分" },
        { min: 500, max: 1000, txt: "500-1000分" },
        { min: 1000, max: 1500, txt: "1000-1500分" },
        { min: 1500, max: 2500, txt: "1500-2500分" },
        { min: 2500, max: 6500, txt: "2500-6500分" },
        { min: 6500, max: 10000, txt: "6500-10000分" },
      ],
      // 分类数组项
      options2: [
        { type: 0, txt: "全部", isActive: true },
        { type: 1, txt: "实体商品", isActive: false },
        { type: 2, txt: "虚拟商品", isActive: false },
      ],
    };
  },
  watch: {
    "$route.query.keyword": {
      handler(newVal, oldVal) {
        if (newVal != oldVal) {
          this.keyword = newVal;
          this.getGoods();
        }
      },
    },
  },
  created() {
    console.log("keyword", this.$route.query.keyword);
    this.keyword = this.$route.query.keyword || "";
    this.getGoods();
  },
  mounted() {
    console.log("添加监听...");
    window.addEventListener("scroll", this.scrollFn);
  },
  beforeDestroy() {
    console.log("取消监听...");
    window.removeEventListener("scroll", this.scrollFn);
  },
  methods: {
    /* 
      getScrollTop      当前滚动条距离
      getClientHeight   可视高度
      getScrollHeight   文档高度
    */
    scrollFn() {
      console.log(
        "滚动了...",
        getScrollTop(),
        getClientHeight(),
        getScrollHeight()
      );
      //1.判断是否加载数据 条件：(当前滚动条距离+可视高度>文档高度  ==>说明到底了)
      if (getScrollTop() + getClientHeight() >= getScrollHeight() - 20) {
        console.log("可以加载数据了");
        
        if (this.goodsShowList.length >= this.goodsList.length) {
          this.isBottom = true;
          return;
        }
        // 解决第三步问题：节流，通过一个变量的控制，让一段代码在一段时间内不会重复执行
        if (this.isLoading == false) {
          this.isLoading = true;
          this.page++;
          //3.解决问题 => 滑动过快时，此处会频繁的进行循环（也可能是发起后台请求）
          //isLoading属性
          setTimeout(() => {
            console.log("开始执行");
            //2.加载数据，一次加载8条(从goodList中获取push到goodsShowList中)
            // 第一页  索引：0~7,  page：1, size：8 
            // 第二页  索引：8~15,  page：2, size：8 
            //公式: 索引开头(size*(page-1))  索引结束(size*page-1)
            for (
              var i = this.size * (this.page - 1);
              i < this.size * this.page;
              i++
            ) {
              this.goodsList[i] && this.goodsShowList.push(this.goodsList[i]);
            }
            console.log("执行完成");
            this.isLoading = false;
          }, 500);
        }
      }
    },
    async getGoods() {
      let res = await GoodsSearchAPI({
        did: this.did,
        type: this.type,
        min: this.min,
        max: this.max,
        keyword: this.keyword,
      });
      console.log("全部商品", res);
      this.goodsList = res.data;
      this.goodsShowList = this.goodsList.filter((item, index) => index < 8);
      console.log(this.goodsList);
      this.page = 1
      this.isBottom = false
      if(this.goodsShowList.length >= this.goodsList){
        this.isBottom = true
      }
    },
    options1Fn(i, min, max) {
      this.optionsNum = i;
      this.min = min;
      this.max = max;
      this.getGoods();
    },
    options2Fn(i, type) {
      this.options2.forEach((item) => {
        item.isActive = false;
      });
      this.options2[i].isActive = true;
      this.type = type;
      this.getGoods();
    },
  },
  components: {
    Crumb,
    List,
  },
};
</script>
 
<style lang = "less" scoped>
@import "../assets/css/public.less";

.goods {
  padding-bottom: 50px;
  background: #efefef;
  padding-top: 20px;
  .options {
    padding-top: 20px;
    padding-bottom: 40px;
    li {
      margin-top: 20px;
      strong {
        color: #000;
        font-weight: bold;
      }
      span {
        margin-right: 20px;
        margin-left: 10px;
        cursor: pointer;
        color: #999;
        &.active {
          color: @base-color;
          font-weight: bold;
        }
      }
    }
  }
}
</style>